/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package acousticfield3d.gui.panels;

import acousticfield3d.gui.MainForm;
import acousticfield3d.math.M;
import acousticfield3d.scene.Entity;
import acousticfield3d.scene.MeshEntity;
import acousticfield3d.scene.Resources;
import acousticfield3d.scene.Scene;
import acousticfield3d.shapes.Quad;
import acousticfield3d.simulation.Slice;
import acousticfield3d.utils.Color;
import acousticfield3d.utils.Parse;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Asier
 */
public class RtSlicePanel extends javax.swing.JPanel {
    
    public static final int Combo_Amplitude = 0;
    public static final int Combo_Phase = 1;
    public static final int Combo_PhaseAndAmp = 2;
    public static final int Combo_TAmpDiffFr= 3;

    final MainForm mf;
    
    public RtSlicePanel(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        sliceAddButton = new javax.swing.JButton();
        sliceDelButton = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        colAmpMinText = new javax.swing.JTextField();
        jLabel32 = new javax.swing.JLabel();
        rtSliceAlphaText = new javax.swing.JTextField();
        rtSliceEnableCheck = new javax.swing.JCheckBox();
        colAmpMaxText = new javax.swing.JTextField();
        amplitudeCheck = new javax.swing.JRadioButton();
        phaseCheck = new javax.swing.JRadioButton();
        ampAndPhaseCheck = new javax.swing.JRadioButton();
        followButton = new javax.swing.JButton();
        clearFollowButton = new javax.swing.JButton();
        stickyCheck = new javax.swing.JCheckBox();

        sliceAddButton.setText("Add");
        sliceAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sliceAddButtonActionPerformed(evt);
            }
        });

        sliceDelButton.setText("Del");
        sliceDelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sliceDelButtonActionPerformed(evt);
            }
        });

        jLabel11.setText("AmpCol:");

        colAmpMinText.setText("0");
        colAmpMinText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colAmpMinTextActionPerformed(evt);
            }
        });

        jLabel32.setText("Alpha:");

        rtSliceAlphaText.setText("1.0");
        rtSliceAlphaText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtSliceAlphaTextActionPerformed(evt);
            }
        });

        rtSliceEnableCheck.setSelected(true);
        rtSliceEnableCheck.setText("enable");
        rtSliceEnableCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtSliceEnableCheckActionPerformed(evt);
            }
        });

        colAmpMaxText.setText("1500");
        colAmpMaxText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colAmpMaxTextActionPerformed(evt);
            }
        });

        buttonGroup1.add(amplitudeCheck);
        amplitudeCheck.setSelected(true);
        amplitudeCheck.setText("Amplitude");
        amplitudeCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                amplitudeCheckActionPerformed(evt);
            }
        });

        buttonGroup1.add(phaseCheck);
        phaseCheck.setText("Phase");
        phaseCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                phaseCheckActionPerformed(evt);
            }
        });

        buttonGroup1.add(ampAndPhaseCheck);
        ampAndPhaseCheck.setText("Amp & Phase");
        ampAndPhaseCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ampAndPhaseCheckActionPerformed(evt);
            }
        });

        followButton.setText("FollowBag");
        followButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                followButtonActionPerformed(evt);
            }
        });

        clearFollowButton.setText("Clear");
        clearFollowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFollowButtonActionPerformed(evt);
            }
        });

        stickyCheck.setText("sticky");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sliceAddButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(sliceDelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rtSliceEnableCheck)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel32)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rtSliceAlphaText, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(colAmpMinText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(colAmpMaxText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(followButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(clearFollowButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(stickyCheck)
                            .addComponent(ampAndPhaseCheck)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(amplitudeCheck)
                                .addGap(18, 18, 18)
                                .addComponent(phaseCheck)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rtSliceEnableCheck)
                    .addComponent(jLabel32)
                    .addComponent(rtSliceAlphaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(colAmpMinText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(colAmpMaxText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(amplitudeCheck)
                    .addComponent(phaseCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ampAndPhaseCheck)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sliceAddButton)
                    .addComponent(sliceDelButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(followButton)
                    .addComponent(clearFollowButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stickyCheck)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void sliceAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sliceAddButtonActionPerformed
        MeshEntity me = createSlice();
        mf.addMeshEntityToSceneCenter(me);
        mf.needUpdate();
    }//GEN-LAST:event_sliceAddButtonActionPerformed

    public MeshEntity createSlice(){
        return new Slice(Resources.MESH_QUAD, null, getSliceShader());
    }
    
    private void sliceDelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sliceDelButtonActionPerformed
        Scene.keepWithTag( mf.selection, Entity.TAG_SLICE);
        mf.scene.getEntities().removeAll( mf.selection );
        mf.clearSelection();
        mf.needUpdate();
    }//GEN-LAST:event_sliceDelButtonActionPerformed

    private void colAmpMinTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colAmpMinTextActionPerformed
        mf.needUpdate();
    }//GEN-LAST:event_colAmpMinTextActionPerformed

    private void rtSliceAlphaTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtSliceAlphaTextActionPerformed
        final int alpha = (int) M.clamp(getRTSliceAlpha() * 256, 0, 255);
        
        final List<Entity> sel = mf.selection;
        for(Entity e : sel){
            if ( e instanceof Slice){
                Slice s = (Slice)e;
                s.setColor( Color.changeAlpha(s.getColor(), alpha) );
            }
        }
        
        mf.needUpdate();
    }//GEN-LAST:event_rtSliceAlphaTextActionPerformed

    private void rtSliceEnableCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtSliceEnableCheckActionPerformed
        Scene.setVisible( mf.scene.getEntities(), Entity.TAG_SLICE, rtSliceEnableCheck.isSelected() );
        mf.needUpdate();
    }//GEN-LAST:event_rtSliceEnableCheckActionPerformed

    private void colAmpMaxTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colAmpMaxTextActionPerformed
        mf.needUpdate();
    }//GEN-LAST:event_colAmpMaxTextActionPerformed

    private void amplitudeCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_amplitudeCheckActionPerformed
        updateSliceSource();
        mf.needUpdate();
    }//GEN-LAST:event_amplitudeCheckActionPerformed

    private void phaseCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phaseCheckActionPerformed
        updateSliceSource();
        mf.needUpdate();
    }//GEN-LAST:event_phaseCheckActionPerformed

    private void ampAndPhaseCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ampAndPhaseCheckActionPerformed
        updateSliceSource();
        mf.needUpdate();
    }//GEN-LAST:event_ampAndPhaseCheckActionPerformed

    private void followButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_followButtonActionPerformed
        if (mf.bag.isEmpty()){
            return;
        }
        
        final Entity firstEntity = mf.bag.get(0);
        final boolean stickyFollow = stickyCheck.isSelected();
        final List<Entity> sel = mf.selection;
        for(Entity e : sel){
            if ( e instanceof Slice){
                Slice s = (Slice)e;
                s.setFollowEntity( firstEntity );
                s.setStickyFollow( stickyFollow );
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_followButtonActionPerformed

    private void clearFollowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearFollowButtonActionPerformed
        final List<Entity> sel = mf.selection;
        for(Entity e : sel){
            if ( e instanceof Slice){
                Slice s = (Slice)e;
                s.clearSticky();
            }
        }
    }//GEN-LAST:event_clearFollowButtonActionPerformed

    public int getSelectSliceSource(){
        if (amplitudeCheck.isSelected()){
            return 0;
        }else if (phaseCheck.isSelected()){
            return 1;
        }else if (ampAndPhaseCheck.isSelected()){
            return 2;
        }
        
        return 0;
    }
    
    private int getSliceShader(){
        final int i = getSelectSliceSource();
        
        if (i == Combo_Amplitude){
            return Resources.SHADER_SLICE_RT_AMP;
        }else if (i == Combo_Phase){
            return Resources.SHADER_SLICE_RT_PHASE;
        }else if (i == Combo_PhaseAndAmp){
            return Resources.SHADER_SLICE_RT_AMPPHASE;
        }
        
        return Resources.SHADER_SLICE_RT_AMP;
    }
        
    private void updateSliceSource(){
        int shader = getSliceShader();
        for(Entity e : mf.selection){
            if (e.getTag() == Entity.TAG_SLICE){
                if (e instanceof MeshEntity){
                    MeshEntity me = (MeshEntity)e;
                    me.setShader( shader );
                }
            }
        }
    }
    
 
            
    public float getAmpColorMin() {
        return Parse.toFloat( colAmpMinText.getText() );
    }
    
    public float getAmpColorMax() {
        return Parse.toFloat( colAmpMaxText.getText() );
    }
    
 
    public float getRTSliceAlpha(){
        return Parse.toFloat( rtSliceAlphaText.getText() );
    }
   
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton ampAndPhaseCheck;
    private javax.swing.JRadioButton amplitudeCheck;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton clearFollowButton;
    private javax.swing.JTextField colAmpMaxText;
    private javax.swing.JTextField colAmpMinText;
    private javax.swing.JButton followButton;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JRadioButton phaseCheck;
    private javax.swing.JTextField rtSliceAlphaText;
    private javax.swing.JCheckBox rtSliceEnableCheck;
    private javax.swing.JButton sliceAddButton;
    private javax.swing.JButton sliceDelButton;
    private javax.swing.JCheckBox stickyCheck;
    // End of variables declaration//GEN-END:variables

    
}
